package com.thunisoft.design.singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @title LazyInnerClassSingleton.java
 * @package com.thunisoft.design.singleton
 * @projectName spring.boot
 * @author yzj
 * @date 2020年5月22日 下午2:29:42
 */
public class LazyInnerClassSingleton implements Serializable {

	/** */
	private static final long serialVersionUID = 1L;

	/** */

	private LazyInnerClassSingleton() {
		if (LazyHolder.INSTANCE != null) {
            throw new RuntimeException("不要试图用反射破坏单例模式");
        }
	}

	public static final LazyInnerClassSingleton getInstance() {
		return LazyHolder.INSTANCE;
	}

	public void print() {
		System.out.println("测试内部类实现单例模式");
	}

	private static class LazyHolder {

		private static final LazyInnerClassSingleton INSTANCE = new LazyInnerClassSingleton();

	}

	private Object readResolve() throws ObjectStreamException {
		// instead of the object we're on,
		// return the class variable INSTANCE
		return LazyHolder.INSTANCE;
	}
}
